<?php
	
	function url() {
		$pageURL = 'http';
		if (@$_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
			$pageURL .= "://";
		if ($_SERVER["SERVER_PORT"] != "80") {
			$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
		} else {
			$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
		}
		$index = (strpos($pageURL,"php")) ? null : "index.php";
		return $pageURL.$index;
	}


	// DB connect
	function dbOpen($dbName = "web2_library") {

		$db = mysql_connect('localhost', 'root', 'sSNs0cQj') or die("Gabim gjat lidhjes me bazen e te dhenave");
		mysql_select_db($dbName,$db) or die("Gabim gjat lidhjes me bazen e te dhenave");
		mysql_query("SET NAMES 'utf8';",$db);
		mysql_query("SET CHARACTER SET 'utf8';",$db);
		return $db;
	}


	// DB disconnect
	function dbClose() {
		mysql_close();
	}


	// Create associative array from db
	function db_indexing($query,$lastOpt="") {

		$query = mysql_query($query);

		while ($row = mysql_fetch_array($query,MYSQL_NUM)) {

			$return[$row[0]]= $row[1];

			// Javascript: Put the last option for javascript script
			if (!empty($lastOpt)) $return['other']= '[$'.$last_opt.']';
		}

		return $return;
	}


	// Password generator
	function generatePassword($length=8) {

		// Vowels and consonants
		$vowels = 'aeuyAEUY';
		$consonants = 'bdghjmnpqrstvzBDGHJLMNPQRSTVWXZ23456789';

		$password = '';
		$alt = time() % 2;

		for ($i = 0; $i < $length; $i++) {
			if ($alt == 1) {
				$password .= $consonants[(rand() % strlen($consonants))];
				$alt = 0;
			} else {
				$password .= $vowels[(rand() % strlen($vowels))];
				$alt = 1;
			}
		}

		return $password;
	}


	// Set Years Array for Select options processing
	function yearArray($maxYears = 80) {

		$thisYear = date("Y");
		$beginYear = $thisYear - $maxYears;

		for ($i = $beginYear; $i <= $thisYear; $i++)  {
			$years[$i] = $i;
		}

		// DESC sort
		arsort($years);

		return $years;
	}


	// Get all dir names from current dir
	function dirPath($path) {

		$TrackDir = opendir($path);

		while ($file = readdir($TrackDir)) {

			if ($file == "." || $file == "..") { }
			else
				$return[] = $file;
		}

		closedir($TrackDir);

		return $return;
	}


	// Most readed books
	function topbooks($sid,$limit=5) {
		
		$query = "SELECT book_id, book_author_id, book_volume, book_title
				  FROM book WHERE book_access != '0' AND book_approve = '0' ";
		
		if (!is_null($sid))
			$query .= "AND book_ection_id = '".$sid."' ";
			
		$query .= "ORDER BY book_counter DESC LIMIT ".$limit."";

		$sql = mysql_query($query);
	
		if (mysql_num_rows($sql) > 0) {
	
			while ($row = mysql_fetch_assoc($sql)) {
	
				$row['title'] = "<b>".$row['book_volume']."</b>".$row['book_title'];
				$row['lead'] = implode(mysql_fetch_assoc(mysql_query("SELECT author_name FROM author WHERE author_id = '".$row['book_author_id']."'")));
				$row['link'] = "{root}library/book.php?id=".$row['book_id'];
				$topbooks[] = $row;
			}
		}

		return $topbooks;
	}


	// Book rating value
	function stars($id,$star=5) {

		$path = "{framework}scripts/jquery/img/raty/";
		$sql = mysql_query("SELECT rating_stars, rating_users FROM rating WHERE rating_book_id='".$id."'");

		if (mysql_num_rows($sql) == 0)
			$score = 0;
		else {
			$result = mysql_fetch_assoc($sql);
			$usrArray = explode(",",$result['rating_users']);
			$count = count($usrArray);
			$currentVote = @number_format($result['rating_stars']/$count,2);
			$increments = 1 / 0.5;
			$score = round($currentVote * $increments) / $increments;
		}

		if (!is_int($score)) $half = true;

		for ($i = 1; $i <= $star; $i++) {
			if ($i <= $score)
				$stars .= "<img src='".$path."star-on.png'>";
			elseif ($half && $i == floor($score) +1)
				$stars .= "<img src='".$path."star-half.png'>";
			else
				$stars .= "<img src='".$path."star-off.png'>";
		}

		return $stars;
	}

	
	// Bookmarks
	function bookmarks($user_id,$sid) {

		$sql = mysql_query("SELECT book.book_id, book.book_author_id, book.book_volume, book.book_title, bookmark.bookmark_favorite
							FROM library, bookmark
							WHERE book.book_id = bookmark.bookmark_book_id AND bookmark.bookmark_user_id = '1' AND book.book_section_id = '".$sid."'
							ORDER BY book.book_counter DESC LIMIT 5");

		if (mysql_num_rows($sql) > 0) {

			while ($row = mysql_fetch_assoc($sql)) {

				// Nese libri eshte shenuar si preferim
				if ($row['favorite']) {

					$row['lead'] = implode(mysql_fetch_assoc(mysql_query("SELECT author FROM authors WHERE id = '".$row['book_author_id']."'")));
					$row['link'] = "book.php?id=".$row['book_id'];
					$row['title'] = "<b>".$row['book_volume']."</b>".$row['book_title']; 
					$return[] = $row;
				}
			}
		}

		return $return;
	}
	
	
	// In pending
	function pending() {
		
		$sql = mysql_query("SELECT pending_id, pending_author, pending_title, pending_description 
							FROM pending WHERE pending_post_date > '".date("Y-m-d",$_SERVER['REQUEST_TIME'])."' ORDER BY pending_post_date LIMIT 5");
	
		if (mysql_num_rows($sql) > 0) {
	
			while ($row = mysql_fetch_assoc($sql)) {
				$row['id'] = $row['pending_id'];
				$row['title'] = $row['pending_title']."<span>".substr($row['pending_description'], 0, 120)."..</span>";
				$row['lead'] = $row['pending_author'];
				$row['link'] = "{root}library/pending.php?id=".$row['pending_id'];
				$pending[] = $row;
			}
		}
		return $pending;
	}


?>